package server.fx;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import server.socket.Server;

public class ServerController implements Initializable {
	
	@FXML
	private TextArea receArea=new TextArea();//接收框
	@FXML
	private Button startBtn=new Button();//开始按钮
	@FXML
	private Button stopBtn=new Button();//停止按钮
	
	private Server server;//服务器后台
	
	private SimpleStringProperty receMsg=new  SimpleStringProperty();//接收消息字符串

	/**
	 * 初始化
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		server=new Server(receMsg);
		
		receMsg.addListener(e->{
		     receArea.setText(receArea.getText()+receMsg.get()+"\n");	
		});
		
	}
	
	/**
	 * 开始按钮事件
	 * @param e
	 */
	@FXML
	public void startBtnOnAction(ActionEvent e) {
		startBtn.setDisable(true);
		stopBtn.setDisable(false);
		new Thread() {
			public void run() {
				server.startListen();
			}
		}.start();
	}
	 
	/**
	 * 停止按钮事件
	 * @param e
	 */
	@FXML
	private void stopBtnOnAction(ActionEvent e) {
		startBtn.setDisable(false);
		stopBtn.setDisable(true);
		new Thread() {
			public void run() {
				server.stopListen();
			}
		}.start();
	}



}
